#pragma once

#include <stdio.h>
#include "global_config.h"
#include "MessageRouter.h"
#include <string>

enum TileFlag
{
	TILE_DISABLED = 0,
	TILE_VISIBLE = 1<<0,
	TILE_COLLISION = 1<<1,
	TILE_DESTRUCTABLE = 1<<2,
	TILE_DESTROYED = 1<<3,
	TILE_BELT = 1<<4
};


struct Destructable
{
	int max_health;
	int cur_health;
	int itemID;
	int itemAmount;
	Destructable()
	{
		max_health = 0;
		cur_health = 0;
		itemID = 0;
		itemAmount = 0;
	}
};


void destructable_set(Destructable * d, Uint8 hp, Uint8 itemID, Uint8 itemAmount );

struct EntityList;
struct Game;

//the amount of tiles in a chunk array
//256 x 256 array
struct Chunk
{
	V2i id;
	Uint32 flags[G::TILES_IN_CHUNK][G::TILES_IN_CHUNK];
	char sprite_index[G::TILES_IN_CHUNK][G::TILES_IN_CHUNK];
	EntityList * entitys;
	//only initialisation tiles that need to be destructables
	Destructable * destructable[G::TILES_IN_CHUNK][G::TILES_IN_CHUNK];
	Chunk( )
	{
		id = v2i(0,0);
		for (int i = 0; i < G::TILES_IN_CHUNK; i++)
			for (int j = 0; j < G::TILES_IN_CHUNK; j++)
			{
				flags[j][i] = 0;
				sprite_index[j][i] = 0;
				destructable[j][i] = 0;
			}
		entitys = 0;
	}
	Chunk(const Chunk& other){printf("Chunk copied");}
};

Chunk * game_chunk_create(V2i chunk_position);

//returns chunk and if chunk is null, creates a new chunk in position
Chunk * game_get_chunk(Chunk * arry[G::CHUNK_COUNT][G::CHUNK_COUNT], V2i chunk_id);

//Chunk * game_get_chunk(Game * game, V2i chunk_id);

V2i game_get_chunk_id(V2 abs_world_pos);

Chunk * game_get_chunk_at_pos(Chunk * arry[G::CHUNK_COUNT][G::CHUNK_COUNT], V2 abs_world_pos);

V2 Xgame_get_abs_pos(V2i chunk_id, V2 offset);

int IsSet(const Chunk * c, V2i tile_index, Uint32 flags);

//points are relative to chunk position
//IS THIS PASSING COUNTxCOUNT pointers?
//TOO many arguments

GLOBAL_CONSTANT int CHUNK_PORTION_SIZE = 10;
struct ChunkPortion
{
	int flags[CHUNK_PORTION_SIZE][CHUNK_PORTION_SIZE];
	V2i size;
	V2i top_left_world_pos;
};


struct Position;
bool Xgame_tile_rect_collision(Chunk * chunk_array[G::CHUNK_COUNT][G::CHUNK_COUNT], const Chunk * chunk, Position pos, V2i size, V2 & out_col_point, bool & found_flag, int flags = 0);

bool game_tile_rect_collision_new(Chunk * chunk_array[G::CHUNK_COUNT][G::CHUNK_COUNT], const Chunk * chunk, Position pos, V2i size);

void get_chunks_on_screen(V2 world_camera_pos, V2i & out_min_cid, V2i & out_max_cid, V2i & out_camera_cid);

namespace OBS{
	struct MessageData;
}



MessageData game_chunk_on_entity_move(void * sender, std::string message, MessageData md);
MessageData game_chunk_on_entity_destroy(void * sender, std::string message, MessageData md);

static void game_chunk_init(MessageRouter * mr)
{
#if 0
	MessageRouter_RegisterTriggerCallback(mr,game_chunk_on_entity_move,"on_move");
	MessageRouter_RegisterTriggerCallback(mr,game_chunk_on_entity_destroy,"on_destroy");
#endif
}


